package router

import (
	"net/http"
	"test/http/frame/controller"
)

type router struct{
	r map[string]http.Handler
}

func (this *router) ServeHTTP(w http.ResponseWriter,r *http.Request){
	for k,v := range this.r{
		if k == r.RequestURI {
			v.ServeHTTP(w,r)
			return
		}
	}
	w.Write([]byte("404"))
}

func (this *router)AddRouter(pattern string, handlerFunc func(http.ResponseWriter, *http.Request)){
	this.r[pattern] = http.HandlerFunc(handlerFunc)
}


var m *router
func init(){
	m = &router{}
	m.r = make(map[string]http.Handler)
}

func Load() http.Handler {
	m.AddRouter("/update_one", controller.UpdateOne)
	m.AddRouter("/update", controller.Update)
	return m
}
